package code.main.modle.table
{
	import code.main.controller.event.EventManager;
	import code.main.controller.event.EventName;
	import code.main.controller.event.MyEvent;
	import code.main.modle.dnAlg.DnAlg;

	/**
	 *玩家数据 
	 * @author YY
	 * 
	 */
	public class Player
	{
		public static var GENDER_MALE:int = 0;//男性
		public static var GENDER_FEMALE:int = 0;//女性
		
		private var _gameId:String;//玩家id
		private var _level:int;    //用户等级
		private var _titleId:int;  //用户称号id 与前台称号表对应
		private var _seatId:int;   //用户座位号
		private var _gender:int;	    //用户性别	1:男 0:女
		private var _vipLevel:int;    //vip等级  0:不是vip 等级越高 vip越nb
		private var _exp:int;		   	//exp用户经验
		private var _giftId:int;       //礼物id
		private var _gameMoney:Number = 0; //游戏币
		private var _platformMoney:Number = 0;//中介币
		private var _tableMoney:Number = 0;	//用户桌子上还剩的钱
		private var _isBetMoney:Number = 0;//已经下的注
		private var _income:Number = 0;//本局进账
		private var _userName:String;     //用户姓名
		private var _pic:String; 	      //用户头像地址
		private var _isSelectCard:Boolean;//是否已经选牌
		private var _cowNumb:int = 10;//牛值   10:没牛，20:有牛,21:牛1....30:牛牛,40:银牛,50:四炸,60:五花,70:五小
		private var _maxCardValue:CardVo;//最大的牌值
		private var _isBanker:Boolean;//是否是庄家
		private var _isSeat:Boolean;//已经坐下
		private var _isGaming:Boolean = false;//是否是在比赛中
		private var _userKey:String = "123456";
		private var _loginCountContinue:int = 1;//连续登陆天数
		
		public var tableVo:TableInfoVo = new TableInfoVo();//桌子信息
		public var cards:Array = [];//玩家的牌
		public function Player()
		{
		}
		
		public function get loginCountContinue():int
		{
			return _loginCountContinue;
		}

		public function set loginCountContinue(value:int):void
		{
			_loginCountContinue = value;
		}

		public function get exp():int
		{
			return _exp;
		}
		
		public function set exp(value:int):void
		{
			_exp = value;
		}
		
		public function get giftId():int
		{
			return _giftId;
		}
		
		public function set giftId(value:int):void
		{
			_giftId = value;
		}
		
		public function get gameMoney():Number
		{
			return _gameMoney;
		}
		
		public function set gameMoney(value:Number):void
		{
			value = Math.max(0, value);
			if(_gameMoney != value) {
				_gameMoney = value;
				var me:MyEvent = new MyEvent(EventName.GAME_MONEY_CHANGE);
				EventManager.getInstance().dispatcher(me);
			}
		}
		
		public function get platformMoney():Number
		{
			return _platformMoney;
		}
		
		public function set platformMoney(value:Number):void
		{
			value = Math.max(0, value);
			if(_platformMoney != value) {
				_platformMoney = value;
				var me:MyEvent = new MyEvent(EventName.PLATFORM_MONEY_CHANGE);
				EventManager.getInstance().dispatcher(me);
			}
		}
		
		public function get tableMoney():Number
		{
			return _tableMoney;
		}
		
		public function set tableMoney(value:Number):void
		{
			_tableMoney = value;
		}
		
		public function get userName():String
		{
			return _userName;
		}
		
		public function set userName(value:String):void
		{
			_userName = value;
		}
		
		public function get pic():String
		{
			return _pic;
		}
		
		public function set pic(value:String):void
		{
			_pic = value;
		}
		
		public function get vipLevel():int
		{
			return _vipLevel;
		}
		
		public function set vipLevel(value:int):void
		{
			_vipLevel = value;
		}
		
		public function get gender():int
		{
			return _gender;
		}
		
		public function set gender(value:int):void
		{
			_gender = value;
		}
		
		public function get seatId():int
		{
			return _seatId;
		}
		
		public function set seatId(value:int):void
		{
			_seatId = value;
		}
		
		public function get titleId():int
		{
			return _titleId;
		}
		
		public function set titleId(value:int):void
		{
			_titleId = value;
		}
		
		public function get level():int
		{
			return _level;
		}
		
		public function set level(value:int):void
		{
			value = Math.max(0, value);
			if(_level != value) {
				_level = value;
				var me:MyEvent = new MyEvent(EventName.LEVEL_CHANGE);
				EventManager.getInstance().dispatcher(me);
			}
		}
		
		public function get gameId():String
		{
			return _gameId;
		}
		
		public function set gameId(value:String):void
		{
			_gameId = value;
		}

		public function get isBetMoney():Number
		{
			return _isBetMoney;
		}

		public function set isBetMoney(value:Number):void
		{
			_isBetMoney = value;
		}

		public function get isSelectCard():Boolean
		{
			return _isSelectCard;
		}

		public function set isSelectCard(value:Boolean):void
		{
			_isSelectCard = value;
		}

		public function get cowNumb():int
		{
			
			return _cowNumb;
		}

		public function set cowNumb(value:int):void
		{
			_cowNumb = value;
		}


		public function get isBanker():Boolean
		{
			return _isBanker;
		}
		
		public function set isBanker(value:Boolean):void
		{
			_isBanker = value;
		}

		public function get isSeat():Boolean
		{
			return _isSeat;
		}

		public function set isSeat(value:Boolean):void
		{
			_isSeat = value;
		}
		
		/**
		 *获得牌的值 
		 * @return 
		 * 
		 */
		public function getCardsValue():Array {
			var i:int;
			var len:int = cards.length;
			var vo:CardVo;
			var arr:Array = [];
			for(i=0; i<len; i++) {
				vo = cards[i];
				arr.push(vo.value);
			}
			return arr;
		}
		
		/**
		 *获得牛值 
		 * @return 
		 * 
		 */
		public function getCowNumb():int {
			return DnAlg.getInstance().getCowValue(cards);
		}
		
		/**
		 *获得 选派的顺序
		 * @return 
		 * 
		 */
		public function getSelectCards():Array {
			var arr:Array = cards;
			var len:int = arr.length;
			var arr1:Array = [];//选择的牌的数组
			var arr2:Array = [];//没有被选择的牌的数组
			var i:int;
			var vo:CardVo;
			var returnArr:Array;
			for(i=0; i<len; i++) {
				vo = arr[i];
				if(vo.selectBoo) {
					arr1.push(vo.value);
				}else {
					arr2.push(vo.value); 
				}
			}
			if(arr1.length == 3){
				returnArr = arr1.concat(arr2);
				return returnArr;
			}
			if(arr2.length == 3) {
				returnArr = arr2.concat(arr1);
				return returnArr;
			}
			return arr1.concat(arr2);
		}
		
		
		/**
		 *获得玩家最大的牌 
		 * @return 
		 * 
		 */
		public function getMaxCardVo():CardVo {
			var vo:CardVo;
			var i:int;
			var arr:Array = cards;
			var len:int = arr.length;
			var maxVo:CardVo = arr[0];
			for(i=1; i<len; i++) {
				if(comparisonCardVo(maxVo,arr[i]) == -1) {
					maxVo = arr[i];
				}
			}
			return maxVo;
		}
		
		/**
		 *比较两张牌的大小 
		 * @param vo1
		 * @param vo2
		 * @return 
		 * 
		 */
		public function comparisonCardVo(vo1:CardVo, vo2:CardVo):int {
			if(vo1.id > vo2.id) {
				return 1;
			}else if(vo1.id == vo2.id){
				if(vo1.color > vo2.color) {
					return 1;
				}
			}
			return -1;
		}
		
		/**
		 *比较两个玩家牌的大小 
		 * @param player
		 * 
		 */
		public function comparisonPlyaer(player:Player):Boolean {
			if(this.cowNumb > player.cowNumb) {
				return true;
			}else if(this.cowNumb == player.cowNumb) {
				if(comparisonCardVo(this.getMaxCardVo(), player.getMaxCardVo()) > 0) {
					return true;
				}
			}
			return false;
		}
		
		/**
		 *活得被选中牌的张数 
		 * @return 
		 * 
		 */
		public function getSelectCardNumb():int {
			var numb:int = 0;
			var vo:CardVo;
			var i:int;
			var arr:Array = cards;
			var len:int = arr.length;
			for(i=0; i<len; i++) {
				vo = arr[i];
				if(vo.selectBoo) {
					numb++;
				}
			}
			return numb;
		}
		
		/**
		 *重设数据 
		 * 
		 */
		public function reset():void {
			_isBetMoney = 0;
			_maxCardValue = null;
			_cowNumb = 10;
			_isSelectCard = false;
		}
		
		/**
		 *用户站起设置数据 
		 * 
		 */
		public function upSeat():void {
			reset();
			_isGaming = false;
			_isSeat = false;
			_gameMoney += _tableMoney>0?_tableMoney:0;
		}
		

		public function get isGaming():Boolean
		{
			return _isGaming;
		}

		public function set isGaming(value:Boolean):void
		{
			_isGaming = value;
		}

		public function get userKey():String
		{
			return _userKey;
		}

		public function set userKey(value:String):void
		{
			_userKey = value;
		}


	}
}